#!/bin/bash
# Script for creating release bundle.
#
# Usage: ./scripts/dev/bundle
#
set -e

commit_tag="$(git tag --points-at HEAD)"
if [[ $commit_tag == "" ]]; then
    echo "No tag for current commit."
    exit -1
fi

docker_tag=${commit_tag:1} # Drop the "v" at the beginning.

release_dir=release-$commit_tag
mkdir -p $release_dir

for ext in "" ".https" ".workers" ".server"; do
    instance_dir=judge0-$commit_tag
    if [[ $ext != "" ]]; then
        instance_dir=$instance_dir-${ext:1}
    fi
    pushd $release_dir
    mkdir -p $instance_dir
    cp ../docker-compose$ext.yml $instance_dir/docker-compose.yml
    cp ../judge0.conf $instance_dir/
    sed -i.bak "s/judge0:latest/judge0:$docker_tag/g" $instance_dir/docker-compose.yml
    rm $instance_dir/docker-compose.yml.bak
    zip -r $instance_dir.zip $instance_dir
    popd
done